package com.ms.demo;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.AutoReplyInfoApi;
import com.jfinal.weixin.sdk.api.CallbackIpApi;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.api.QrcodeApi;
import com.jfinal.weixin.sdk.api.ShorturlApi;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.jfinal.weixin.sdk.utils.HttpUtils;

public class WeixinApiController extends ApiController {

	/**
	 * 获取公众号菜单
	 */
	public void getMenu() {
		ApiResult apiResult = MenuApi.getMenu();
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 创建菜单
	 */
	public void createMenu()
	{
//		String str = "{\n" +
//				"    \"button\": [\n" +
//				"        {\n" +
//				"            \"name\": \"进入理财\",\n" +
//				"            \"url\": \"http://m.bajie8.com/bajie/enter\",\n" +
//				"            \"type\": \"view\"\n" +
//				"        },\n" +
//				"        {\n" +
//				"            \"name\": \"安全保障\",\n" +
//				"            \"key\": \"112\",\n" +
//				"\t    \"type\": \"click\"\n" +
//				"        },\n" +
//				"        {\n" +
//				"\t    \"name\": \"使用帮助\",\n" +
//				"\t    \"url\": \"http://m.bajie8.com/footer/cjwt\",\n" +
//				"\t    \"type\": \"view\"\n" +
//				"        }\n" +
//				"    ]\n" +
//				"}";

	    //CHANG BA URL
		Map<String, String> menu_item = new HashMap<>();
	    menu_item.put("name", "在线预订");
	    menu_item.put("type", "view");
	    menu_item.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc4d213c8398761cf&redirect_uri=http://wechat.changba-ktv.com/reservation/ktvlist?&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");

		Map<String, String> menu_item1 = new HashMap<>();
		menu_item1.put("name", "KTV点歌");
		menu_item1.put("type", "view");
		menu_item1.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc4d213c8398761cf&redirect_uri=http://wechat.changba-ktv.com/songconsole/main?&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
	  
		List<Map> subButtonArr=new ArrayList<>();
	    Map<String, String>  submenu_item0 =new HashMap<>();
	    submenu_item0.put("name", "店面查询");
	    submenu_item0.put("type", "view");
	    submenu_item0.put("url", "http://tech002.changba-ktv.com:8066/stores.aspx");
	    
	    Map<String, String>  submenu_item2 =new HashMap<>();
	    submenu_item2.put("name", "我的信息");
	    submenu_item2.put("type", "view");
	    submenu_item2.put("url", "https://ktvs.changba.com/wx/mysong_openid.php?api=myinfo&key=4DCFFC1FE05AC63E0FE9990BAB5B8AFD");

	    Map<String, String>  submenu_item3 =new HashMap<>();
	    submenu_item3.put("name", "手机绑定");
	    submenu_item3.put("type", "click");
	    submenu_item3.put("key", "MYSONG_WXMOBILEBIND");


	    Map<String, String>  submenu_item4 =new HashMap<>();
	    submenu_item4.put("name", "我的优惠券");
	    submenu_item4.put("type", "view");
	    submenu_item4.put("url", "http://wechat.changba-ktv.com/erpBase/myerpBase");

	    Map<String, String>  submenu_item5 =new HashMap<>();
	    submenu_item5.put("name", "我的订单");
	    submenu_item5.put("type", "view");
	    submenu_item5.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc4d213c8398761cf&redirect_uri=http://wechat.changba-ktv.com/order/appmyorder?&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");

	    subButtonArr.add(submenu_item0);
	    subButtonArr.add(submenu_item2);
	    subButtonArr.add(submenu_item3);
	    subButtonArr.add(submenu_item4);
	    subButtonArr.add(submenu_item5);
	    
	    Map menu_item2 = new HashMap<>();
	    menu_item2.put("name", "更多");
	    menu_item2.put("sub_button", subButtonArr);
	    
	    
	    List resultArr=new ArrayList<>();
	    resultArr.add(menu_item);
	    resultArr.add(menu_item1);
	    resultArr.add(menu_item2);
	    
	    Map resultMap=new HashMap<>();
	    resultMap.put("button", resultArr);
	    
	    String str=JSON.toJSONString(resultMap);
		
	    System.out.println(str);
	    MenuApi.deleteMenu();
		ApiResult apiResult = MenuApi.createMenu(str);
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 获取公众号关注用户
	 */
	public void getFollowers()
	{
		ApiResult apiResult = UserApi.getFollows();
		renderText(apiResult.getJson());
	}

	/**
	 * 获取用户信息
	 */
	public void getUserInfo()
	{
		ApiResult apiResult = UserApi.getUserInfo("ohbweuNYB_heu_buiBWZtwgi4xzU");
		renderText(apiResult.getJson());
	}

	public void getAutoReply(){
		ApiResult apiResult = AutoReplyInfoApi.getCurrent();
		renderText(apiResult.getJson());
		
	}
	
	/**
	 * 发送模板消息
	 */
	public void sendMsg()
	{
		String str = " {\n" +
				"           \"touser\":\"ohbweuNYB_heu_buiBWZtwgi4xzU\",\n" +
				"           \"template_id\":\"9SIa8ph1403NEM3qk3z9-go-p4kBMeh-HGepQZVdA7w\",\n" +
				"           \"url\":\"http://www.sina.com\",\n" +
				"           \"topcolor\":\"#FF0000\",\n" +
				"           \"data\":{\n" +
				"                   \"first\": {\n" +
				"                       \"value\":\"恭喜你购买成功！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keyword1\":{\n" +
				"                       \"value\":\"去哪儿网发的酒店红包（1个）\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keyword2\":{\n" +
				"                       \"value\":\"1元\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"remark\":{\n" +
				"                       \"value\":\"欢迎再次购买！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   }\n" +
				"           }\n" +
				"       }";
		ApiResult apiResult = TemplateMsgApi.send(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取参数二维码
	 */
	public void getQrcode()
	{
//		String str = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
//		ApiResult apiResult = QrcodeApi.create(str);
//		renderText(apiResult.getJson());

        String str = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"123@@abc\"}}}";
        ApiResult apiResult = QrcodeApi.create(str);
        String ticket=apiResult.get("ticket");
        
       BufferedInputStream input=  HttpUtils.downloadInputStream("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket);
       String filePath="D:";
        FileOutputStream os = null;
		try {
			os = new FileOutputStream(filePath+"\\myQrfile.jpg");
			byte[] buffer = new byte[1024];  
		    int len = -1;  
		    while ((len = input.read(buffer)) != -1) {  
		    	os.write(buffer, 0, len);  
		    }  
			os.close();  
			input.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				os.close();
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
		} 
          renderText(apiResult.getJson());
	}

	/**
	 * 长链接转成短链接
	 */
	public void getShorturl()
	{
		String str = "{\"action\":\"long2short\"," +
				"\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}";
		ApiResult apiResult = ShorturlApi.getShorturl(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取客服聊天记录
	 */
	public void getRecord()
	{
		String str = "{\n" +
				"    \"endtime\" : 987654321,\n" +
				"    \"pageindex\" : 1,\n" +
				"    \"pagesize\" : 10,\n" +
				"    \"starttime\" : 123456789\n" +
				" }";
		ApiResult apiResult = CustomServiceApi.getRecord(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取微信服务器IP地址
	 */
	public void getCallbackIp()
	{
		ApiResult apiResult = CallbackIpApi.getCallbackIp();
		renderText(apiResult.getJson());
	}
}

